package com.sunny.ihealth.data.im;

import android.util.Log;

import com.google.gson.Gson;
import com.sunny.ihealth.entity.app.Name;
import com.sunny.ihealth.entity.eventbus.RunDateEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import cn.bmob.newim.bean.BmobIMConversation;
import cn.bmob.newim.bean.BmobIMExtraMessage;
import cn.bmob.newim.bean.BmobIMMessage;
import cn.bmob.newim.bean.BmobIMTextMessage;
import cn.bmob.newim.bean.BmobIMUserInfo;
import cn.bmob.newim.core.BmobIMClient;
import cn.bmob.newim.event.MessageEvent;
import cn.bmob.newim.event.OfflineMessageEvent;
import cn.bmob.newim.listener.BmobIMMessageHandler;

/**
 * 聊天消息接收器
 * Created by Sunny  An on 2016/6/6.
 */
public class IMMessageHandler extends BmobIMMessageHandler {

    //当接收到服务器发来的消息时，此方法被调用
    @Override
    public void onMessageReceive(MessageEvent messageEvent) {
        super.onMessageReceive(messageEvent);
        BmobIMMessage msg = messageEvent.getMessage();
        if ("run".equals(msg.getContent())) {
            BmobIMUserInfo bmobIMUserInfo = messageEvent.getFromUserInfo();
            bmobIMUserInfo.setName(new Gson().fromJson(msg.getExtra(), Name.class).getName());
            EventBus.getDefault().post(new RunDateEvent(bmobIMUserInfo));
        }
    }


    //每次调用connect方法时会查询一次离线消息，如果有，此方法会被调用
    @Override
    public void onOfflineReceive(OfflineMessageEvent offlineMessageEvent) {
        super.onOfflineReceive(offlineMessageEvent);
    }

}
